//
//  CellForVideo.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForVideo.h"
#import "ModelForAll.h"
#import "CellForVideoSidList.h"
#import "CellForVideoList.h"
#import <MJRefresh/MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "ModelForForVideoCollect.h"
#import "DataBaseForVideo.h"
#import "NetworkingHandler.h"

@interface CellForVideo ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tabView;
@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, retain) NSMutableArray *arrTitle1;
@property (nonatomic, retain) NSMutableArray *arrDescription;
@property (nonatomic, retain) NSMutableArray *arrCover;
@property (nonatomic, retain) NSMutableArray *arrMp4_url;
@property (nonatomic, retain) NSMutableArray *arrImgsrc;
@property (nonatomic, retain) NSMutableArray *arrLength;
@property (nonatomic, retain) NSMutableArray *arrPlayCount;
@property (nonatomic, retain) NSMutableArray *arrReplyCount;
@property (nonatomic, retain) NSMutableArray *arrVid;
@property (nonatomic, retain) NSMutableArray *arrForCollect;
@property (nonatomic, assign) BOOL isSelect;
@property (nonatomic, assign) NSInteger page;

@end

@implementation CellForVideo

#pragma mark 重写dealloc
- (void)dealloc
{
    [super dealloc];
    
    [_tabView release];
    
    [_arrTitle release];
    
    [_arrTitle1 release];
    
    [_arrDescription release];
    
    [_arrCover release];
    
    [_arrMp4_url release];
    
    [_arrImgsrc release];
    
    [_arrLength release];
    
    [_arrPlayCount release];
    
    [_arrReplyCount release];
    
    [_arrForCollect release];
    
    [_arrVid release];
}




#pragma mark 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil)
    {
        //创建子控件
        [self initialization];
        
        [self handleData];
        
        [self createTabView];
        
        [[DataBaseForVideo shareDataBaseHandle]open];
        [[DataBaseForVideo shareDataBaseHandle] createTable];
        
        
        self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            self.page = 0;
            
            [self removeArrs];
            
            [self handleData];
            
        }];

        
       
        
        
        [[NSNotificationCenter defaultCenter] addObserverForName:@"refresh" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            
            [self.tabView reloadData];
            
        }];
        
    }
    return self;
}




#pragma mark 子控件的布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    //collectionCell 的子控件的布局通常在此方法中设置
    [super applyLayoutAttributes:layoutAttributes];
    
    //设置tabView的空间位置及大小
    self.tabView.frame = self.frame;
}




#pragma mark 自定义方法 >>>>>>
#pragma mark 初始化
- (void)initialization
{
    self.backgroundColor = [UIColor whiteColor];
    //初始化数组
    self.arrTitle = [NSMutableArray array];
    
    self.arrCover = [NSMutableArray array];
    
    self.arrMp4_url = [NSMutableArray array];
    
    self.arrDescription = [NSMutableArray array];
    
    self.arrLength = [NSMutableArray array];
    
    self.arrPlayCount = [NSMutableArray array];
    
    self.arrReplyCount = [NSMutableArray array];
    
    self.arrVid = [NSMutableArray array];
    
    self.arrForCollect = [NSMutableArray array];
}


- (void)removeArrs
{
    [self.arrTitle removeAllObjects];
    
    [self.arrCover removeAllObjects];
    
    [self.arrMp4_url removeAllObjects];
    
    [self.arrDescription removeAllObjects];
    
    [self.arrLength removeAllObjects];
    
    [self.arrPlayCount removeAllObjects];
    
    [self.arrReplyCount removeAllObjects];
    
    [self.arrVid removeAllObjects];
    
    [self.arrForCollect removeAllObjects];
}



#pragma mark 解析网络数据
- (void)handleData
{
    //设置访问地址
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld-10.html", 10 * self.page++];
    
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        ModelForAll *model = [[ModelForAll alloc] init];
        
        [model setValuesForKeysWithDictionary:result];
        
        self.arrTitle1 = [NSMutableArray array];
        
        self.arrImgsrc = [NSMutableArray array];
        
        for (NSDictionary *dic in model.videoSidList)
        {
            NSString *str1 = [dic objectForKey:@"imgsrc"];
            
            NSString *str2 = [dic objectForKey:@"title"];
            
            [self.arrImgsrc addObject:str1];
            
            [self.arrTitle1 addObject:str2];
        }
        
        for (NSDictionary *dic in model.videoList)
        {
            ModelForForVideoCollect *model1 = [[ModelForForVideoCollect alloc] init];
            
            [model1 setValuesForKeysWithDictionary:dic];
            
            [self.arrForCollect addObject:model1];
            
            [model1 release];
            
            NSString *str1 = [dic objectForKey:@"title"];
            
            NSString *str2 = [dic objectForKey:@"description"];
            
            NSString *str3 = [dic objectForKey:@"cover"];
            
            NSString *str4 = [dic objectForKey:@"mp4_url"];
            
            NSString *str5 = [dic objectForKey:@"length"];
            
            NSString *str6 = [dic objectForKey:@"playCount"];
            
            NSString *str7 = [dic objectForKey:@"replyCount"];
            
            NSString *str8 = [dic objectForKey:@"vid"];
            
            [self.arrTitle addObject:str1];
            
            [self.arrDescription addObject:str2];
            
            [self.arrCover addObject:str3];
            
            [self.arrMp4_url addObject:str4];
            
            [self.arrLength addObject:str5];
            
            [self.arrPlayCount addObject:str6];
            
            [self.arrReplyCount addObject:str7];
            
            [self.arrVid addObject:str8];
            
            
            [self.tabView reloadData];
            
            self.tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                
                // 进入刷新状态后会自动调用这个block
                [self handleData];
            }];
            
            [self.tabView.mj_header endRefreshing];
            
            [self.tabView.mj_footer endRefreshing];
        }

    }];
}




#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] init];
    
    [self.contentView addSubview:self.tabView];
    
    [_tabView release];
    
    //指定代理人
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    
    //注册Cell
    [self.tabView registerClass:[CellForVideoList class] forCellReuseIdentifier:@"pool1"];
    [self.tabView registerClass:[CellForVideoSidList class] forCellReuseIdentifier:@"pool2"];
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrMp4_url.count + 1;
}




#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        CellForVideoSidList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        cell.arrImgsrc = self.arrImgsrc;
        
        cell.arrTitle1 = self.arrTitle1;
        
        [cell.collection reloadData];
        
        return cell;
    }
    else
    {
        
        
        
        
        CellForVideoList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        
        //去掉选择cell之后背景颜色灰的效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.arrForCollect = self.arrForCollect;
        
        cell.number = indexPath.row - 1;
        
        ModelForForVideoCollect *model2 = self.arrForCollect[indexPath.row - 1];
        
        self.isSelect =  [[DataBaseForVideo shareDataBaseHandle]select:model2];
        if (self.isSelect)
        {
            [cell.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateNormal];
        }
        else
        {
            [cell.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"取消收藏"] forState:UIControlStateNormal];
        }

        
        
        
        
        NSString *timeLength = self.arrLength[indexPath.row - 1];
        
        cell.lableForTimeLength.text = [[NSString alloc] initWithFormat:@"%02d:%02d", timeLength.intValue / 60, timeLength.intValue % 60];
        
        NSString *playCount = self.arrPlayCount[indexPath.row - 1];
        
        if (playCount.intValue >= 10000)
        {
            cell.lableForPlayCount.text = [NSString stringWithFormat:@"%.1f万", playCount.floatValue / 10000];
        }
        else
        {
            cell.lableForPlayCount.text = [NSString stringWithFormat:@"%d", playCount.intValue];
        }
        
        NSString *replyCount = self.arrReplyCount[indexPath.row - 1];
        
        cell.lableForReplyCount.text = [NSString stringWithFormat:@"%d", replyCount.intValue];
        
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.arrCover[indexPath.row - 1]]];
        
        cell.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row - 1];
        
        cell.lableForDescription.text = [self.arrDescription objectAtIndex:indexPath.row - 1];
        
        return cell;
    }
}




#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 100;
    }
    else
        return 310;
}




#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Video" object:nil userInfo:@{@"item" : self.arrMp4_url[indexPath.row - 1], @"title" : self.arrTitle[indexPath.row - 1], @"vid" : self.arrVid[indexPath.row - 1]}];
}
@end
